/*
京信工具类
作者：小花狗
 */

/*
用于多图片、多文件上传自定义标签，上传成功后填充文件id到**_fileIds元素中
 */
function fillIdForUpload(elementId, fileId) {
    if ($("#" + elementId)) {
        var ids = $("#" + elementId).val();
        if (ids != null && ids != '') {
            var idArray = ids.split(",");
            idArray.push(fileId);
            $("#" + elementId).val(idArray.join(","));
        } else {
            $("#" + elementId).val(fileId);
        }
    }
}

/*
用于多图片、多文件上传自定义标签，删除成功后更新文件id到**_fileIds元素中
 */
function deleteFileForUpload(fileId, fileUrl) {
    layer.confirm('确定要删除该文件吗？删除后将无法恢复！', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: ctx + "/common/sysFile/remove",
            type: "post",
            data: {
                'id': fileId,
                'url': fileUrl
            },
            success: function (r) {
                if (r.code == 0) {
                    var fileIdsInput = $("#upload-files-" + fileId).parent().parent().parent().parent().find("input[name$='FileIds']");
                    var fileIds = fileIdsInput.val().split(",");
                    for (var i = 0; i < fileIds.length; i++) {
                        if (fileIds[i] == fileId) {
                            fileIds.splice(i, 1);
                            break
                        }
                    }
                    fileIdsInput.val(fileIds.join(","));
                    $("#upload-files-" + fileId).remove();
                    layer.msg(r.msg);
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

//下载文件
function downloadFile(fileUrl) {
    fileUrl = fileUrl.replace("files", "");
    // 创建隐藏的可下载链接
    var aLink = document.createElement('a');
    var src = ctx + "/manage/file/file/downloadByUrl?fileName=" + fileUrl;
    aLink.download = src;
    aLink.style.display = 'none';
    aLink.href = src;
    document.body.appendChild(aLink);
    aLink.click();
    document.body.removeChild(aLink);
}

//下载文件
function downloadFileForUpload(fileId, fileUrl, fileName) {
    fileUrl = fileUrl.replace("files", "");
    location.href = ctx + "/common/sysFile/downloadFileByfileUrl?fileName=" + fileUrl;
}

//下载文件
function downloadFileForUploadByUrl(fileUrl, fileName) {
    fileUrl = fileUrl.replace("files", "");
    location.href = ctx + "/common/sysFile/downloadFileByfileUrl?fileName=" + fileUrl;
}
